package com.example.classconnect.fragments;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.example.classconnect.ClassAdapter;
import com.example.classconnect.R;
import com.example.classconnect.UpdateTable;
import com.example.classconnect.User;


public class AddNewClassFragment extends Fragment {
	public AddNewClassFragment(){};
	private User currentUser;
	private ArrayList<String> classes;
	private EditText enteredClass;
	private ListView classList;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
        View rootView = inflater.inflate(R.layout.fragment_addnewclass, container, false);
		classList = (ListView) rootView.findViewById(R.id.classes);
        currentUser = getArguments().getParcelable("CURRENTUSER");
        classes = currentUser.getClassList();
        enteredClass = (EditText) rootView.findViewById(R.id.enterClass);
        ArrayAdapter<String> adapter
        
        = new ArrayAdapter<String>(getActivity(),
      
             android.R.layout.simple_list_item_1,
      
             classes);
      
        classList.setAdapter(adapter);
        rootView.findViewById(R.id.submitClass).setOnClickListener(
				
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						updateUser();
					}
				});
        return rootView;
    }
	public void updateUser(){
		
		currentUser.addClass(enteredClass.getText().toString());
		Intent myIntent = new Intent(getActivity(), UpdateTable.class);
		myIntent.putExtra("UPDATEUSER", currentUser);
		getActivity().startActivity(myIntent);
		getActivity().finish();
	}
	
}
